clear all
cap log close

// The codes comment out here are used for generate the intermediate input data. Users should run them the first time and then leave them comment out. 

/*
// Generate data for 2010 for whole pop
use "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_2011.dta", clear
keep shnro ptoim1 
ren ptoim1 ptoim10

merge 1:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_2010.dta", ///
		keepusing(shnro ptoim1 ika sukup ututku)  		
drop if ptoim1~="11" // must be employed in year -1
keep if _merge==3

*Education variables
g educ=ututku 
destring educ, replace

gen ed_broad =3 if educ>=600000
replace ed_broad=2 if educ>=300000 & educ < 600000
replace ed_broad=1 if missing(educ)
	
* Gender 
destring sukup, replace
keep shnro ika ed_broad sukup
ren ika ika0
ren sukup sukup0
ren ed_broad ed_broad0
save "$dataout\descripE_ids_2010.dta", replace // save ids for creating data 

local time_year = -5
forvalues i= 2006(1) 2010{
	
	use "${data}fleed_kokonais_`i'.dta", clear
	
	// only keep the people in 2010 who employed 
	keep shtun shnro vuosi sykstun syrtun ptoim1 tyotu svatva toimiala ammattikoodi tyokk kunta ututku ika tyrtu svatvp sukup
	merge 1:1 shnro using "$dataout\descripE_ids_2010.dta"
	keep if _merge==3
	drop _merge

	*Industry 
	cap rename toimiala tol95xht
	rename tyotu ttyotul
	rename ammattikoodi amko
	
	*Employment
	destring ptoim1, replace
	gen employed=ptoim1==11 & sykstun!="."

	* Earnings
	replace ttyotul = 0 if missing(ttyotul)
	replace tyrtu = 0 if missing(tyrtu)
	replace svatva = 0 if missing(svatva)
	rename svatvp capitalEarnings
	replace capitalEarnings = 0 if missing(capitalEarnings)

	* Sum labor and enrepreneur earnings 
	gen allEarnings =  ttyotul + tyrtu
	
	g year=vuosi
	fmerge m:1 year using  "${dataout}\cpi"
	keep if _merge == 3 
	drop _merge
	
	gen real_earn=ttyotul/cpi
	gen real_inc=svatva/cpi
	gen realEpreEarn = tyrtu/cpi
	gen realAllEarn = allEarnings/cpi
	gen realCapEarn = capitalEarnings/cpi
	drop cpi
	
	gen time = `time_year'
	
	gen wp_crime=0
	ren year year_event
	keep shnro employed realAllEarn time wp_crime year_event
	
	save "${dataout}descripE_2010pop_`i'", replace
	local time_year = `time_year' +1 
		
}

use "${dataout}descripE_2010pop_2010", clear
forvalues i = 2009(-1)2006 {
    append using "${dataout}descripE_2010pop_`i'"
}	
merge m:1 shnro using "$dataout\descripE_ids_2010.dta"
drop _merge
ren ika0 ika
ren sukup0 sukup
ren ed_broad0 ed_broad
save "${dataout}descripE_2010pop_all", replace
*/
* WEIGHTS and WORKPLACE - VICTIM 
use "${dataout}victim_allmatches_allyears_matchpast5", clear
gen new_wp_crime=victim_sykstun_lag==defendant_sykstun_lag & defendant_sykstun_lag!=""
bys match_id1 year_event: ereplace new_wp_crime=max(new_wp_crime)
keep if new_wp_crime==1 

destring suspect_sex, replace 
destring plaintiff_sex, replace
drop if suspect_sex==2
	
gen mm=suspect_sex==1 & plaintiff_sex==1
gen mf=suspect_sex==1 & plaintiff_sex==2

drop if wp_crime==0
keep if time<=-1

*Education variables
gen ed_broad =3 if educ>=600000
replace ed_broad=2 if educ>=300000 & educ < 600000
replace ed_broad=1 if missing(educ)
destring sukup, replace

// Create weights 
g obs=1
preserve 
	keep if time==-1
	keep if plaintiff_sex==1
	egen total_obs = total(obs)
	collapse (sum) obs (mean) total_obs, by(ika ed_broad)
	gen victim_weight = obs/total_obs
	g mf = 0
	save "$dataout\victim_wp_violence_weight_mm.dta", replace
restore
preserve 
	keep if time==-1
	keep if plaintiff_sex==2
	egen total_obs = total(obs)
	collapse (sum) obs (mean) total_obs, by(ika ed_broad)
	gen victim_weight = obs/total_obs
	g mf = 1
	save "$dataout\victim_wp_violence_weight_mf.dta", replace
restore

append using "${dataout}descripE_2010pop_all"

// Create weight variables 
bys shnro wp_crime : gen tmp = _N
drop if tmp < 5 & wp_crime==0
bys ika ed_broad sukup wp_crime time : gen freq_n = _N
bys sukup wp_crime time : gen freq_N = _N
g pop_weight = freq_n/freq_N
g time_ = time + 5

// Female victims 
preserve 
set scheme s1mono
	drop if wp_crime==1 & plaintiff_sex==1
	drop if wp_crime==0 & sukup == 1
	
	merge m:1 ika ed_broad using "$dataout\victim_wp_violence_weight_mf.dta", keepusing(victim_weight)
	drop if wp_crime==0 & _merge<3
	drop _merge
	
	g correct_factor = victim_weight/pop_weight
	
	mean realAllEarn if wp_crime==1, over(time_)
	est sto victim1_earn
	mean realAllEarn if wp_crime==0 [pw = correct_factor], over(time_)
	est sto victim0_earn
	
	coefplot (victim1_earn, label("Victim")) (victim0_earn, label("Population")), ///
			vertical recast(connected) nooffset graphregion(color(white)) ///
			xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1") xtitle("Time") ytitle("Real Earnings") ///
			name(victim_mf, replace) yscale(r(10000 50000)) ylabel(10000(10000)50000)
			gr export "${results}\descrip_victim_mf.pdf", replace
			gr export "${results}\descrip_victim_mf.eps", replace
			
	mean employed if wp_crime==1, over(time_)
	est sto victim1_earn
	mean employed if wp_crime==0 [pw = correct_factor], over(time_)
	est sto victim0_earn
	
	coefplot (victim1_earn, label("Victim")) (victim0_earn, label("Population")), ///
			vertical recast(connected) nooffset graphregion(color(white)) ///
			xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1") xtitle("Time") ytitle("Employment") ///
			name(victim_mf, replace) yscale(r(0 1)) ylabel(0(0.2)1)
			gr export "${results}\descrip_victim_mf_emp.pdf", replace
			gr export "${results}\descrip_victim_mf_emp.eps", replace

restore 
// Female victims 
preserve 
set scheme s1mono
	drop if wp_crime==1 & plaintiff_sex==2
	drop if wp_crime==0 & sukup == 2
	
	merge m:1 ika ed_broad using "$dataout\victim_wp_violence_weight_mm.dta", keepusing(victim_weight)
	drop if wp_crime==0 & _merge<3
	drop _merge
	
	g correct_factor = victim_weight/pop_weight
	
	mean realAllEarn if wp_crime==1, over(time_)
	est sto victim1_earn
	mean realAllEarn if wp_crime==0 [pw = correct_factor], over(time_)
	est sto victim0_earn
	
	coefplot (victim1_earn, label("Victim")) (victim0_earn, label("Population")), ///
			vertical recast(connected) nooffset graphregion(color(white)) ///
			xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1") xtitle("Time") ytitle("Real Earnings") ///
			name(victim_mm, replace) yscale(r(10000 50000)) ylabel(10000(10000)50000)
			gr export "${results}\descrip_victim_mm.pdf", replace
			gr export "${results}\descrip_victim_mm.eps", replace
			
	mean employed if wp_crime==1, over(time_)
	est sto victim1_earn
	mean employed if wp_crime==0 [pw = correct_factor], over(time_)
	est sto victim0_earn
	
	coefplot (victim1_earn, label("Victim")) (victim0_earn, label("Population")), ///
			vertical recast(connected) nooffset graphregion(color(white)) ///
			xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1") xtitle("Time") ytitle("Employment") ///
			name(victim_mf, replace) yscale(r(0 1)) ylabel(0(0.2)1)
			gr export "${results}\descrip_victim_mm_emp.pdf", replace
			gr export "${results}\descrip_victim_mm_emp.eps", replace

restore 

// Suspect

* WEIGHTS and WORKPLACE - SUSPECT
use "${dataout}allmatches_allyears_matchpast5", clear
gen new_wp_crime=victim_sykstun_lag==defendant_sykstun_lag & defendant_sykstun_lag!=""
bys match_id1 year_event: ereplace new_wp_crime=max(new_wp_crime)
keep if new_wp_crime==1 

destring suspect_sex, replace 
destring plaintiff_sex, replace
drop if suspect_sex==2
	
gen mm=suspect_sex==1 & plaintiff_sex==1
gen mf=suspect_sex==1 & plaintiff_sex==2

drop if wp_crime==0
keep if time<=-1

*Education variables
gen ed_broad =3 if educ>=600000
replace ed_broad=2 if educ>=300000 & educ < 600000
replace ed_broad=1 if missing(educ)
destring sukup, replace

// Create weights 
g obs=1
preserve 
	keep if time==-1
	keep if plaintiff_sex==1
	egen total_obs = total(obs)
	collapse (sum) obs (mean) total_obs, by(ika ed_broad)
	gen suspect_weight = obs/total_obs
	g mf = 0
	save "$dataout\suspect_wp_violence_weight_mm.dta", replace
restore
preserve 
	keep if time==-1
	keep if plaintiff_sex==2
	egen total_obs = total(obs)
	collapse (sum) obs (mean) total_obs, by(ika ed_broad)
	gen suspect_weight = obs/total_obs
	g mf = 1
	save "$dataout\suspect_wp_violence_weight_mf.dta", replace
restore

append using "${dataout}descripE_2010pop_all"
drop if wp_crime==0 & sukup==2 // only keep men 

// Create weight variables 
bys shnro wp_crime : gen tmp = _N
drop if tmp < 5 & wp_crime==0
bys ika ed_broad sukup wp_crime time : gen freq_n = _N
bys sukup wp_crime time : gen freq_N = _N
g pop_weight = freq_n/freq_N
g time_ = time + 5

// Female victims 
preserve 
set scheme s1mono
	drop if wp_crime==1 & plaintiff_sex==1

	merge m:1 ika ed_broad using "$dataout\suspect_wp_violence_weight_mf.dta", keepusing(suspect_weight)
	drop if wp_crime==0 & _merge<3
	drop _merge
	
	g correct_factor = suspect_weight/pop_weight
	
	mean realAllEarn if wp_crime==1, over(time_)
	est sto suspect1_earn
	mean realAllEarn if wp_crime==0 [pw = correct_factor], over(time_)
	est sto suspect0_earn
	
	coefplot (suspect1_earn, label("Suspect")) (suspect0_earn, label("Population")), ///
			vertical recast(connected) nooffset graphregion(color(white)) ///
			xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1") xtitle("Time") ytitle("Real Earnings") ///
			name(suspect_mf, replace) yscale(r(10000 50000)) ylabel(10000(10000)50000)
			gr export "${results}\descrip_suspect_mf.pdf", replace
			gr export "${results}\descrip_suspect_mf.eps", replace
			
	mean employed if wp_crime==1, over(time_)
	est sto suspect1_earn
	mean employed if wp_crime==0 [pw = correct_factor], over(time_)
	est sto suspect0_earn
	
	coefplot (suspect1_earn, label("Suspect")) (suspect0_earn, label("Population")), ///
			vertical recast(connected) nooffset graphregion(color(white)) ///
			xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1") xtitle("Time") ytitle("Employment") ///
			name(suspect_mf, replace) yscale(r(0 1)) ylabel(0(0.2)1)
			gr export "${results}\descrip_suspect_mf_emp.pdf", replace
			gr export "${results}\descrip_suspect_mf_emp.eps", replace

restore 
// Female victims 
preserve 
set scheme s1mono
	drop if wp_crime==1 & plaintiff_sex==2
	
	merge m:1 ika ed_broad using "$dataout\suspect_wp_violence_weight_mm.dta", keepusing(suspect_weight)
	drop if wp_crime==0 & _merge<3
	drop _merge
	
	g correct_factor = suspect_weight/pop_weight
	
	mean realAllEarn if wp_crime==1, over(time_)
	est sto suspect1_earn
	mean realAllEarn if wp_crime==0 [pw = correct_factor], over(time_)
	est sto suspect0_earn
	
	coefplot (suspect1_earn, label("Suspect")) (suspect0_earn, label("Population")), ///
			vertical recast(connected) nooffset graphregion(color(white)) ///
			xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1") xtitle("Time") ytitle("Real Earnings") ///
			name(suspect_mm, replace) yscale(r(10000 50000)) ylabel(10000(10000)50000)
			gr export "${results}\descrip_suspect_mm.pdf", replace
			gr export "${results}\descrip_suspect_mm.eps", replace
			
	mean employed if wp_crime==1, over(time_)
	est sto suspect1_earn
	mean employed if wp_crime==0 [pw = correct_factor], over(time_)
	est sto suspect0_earn
	
	coefplot (suspect1_earn, label("Suspect")) (suspect0_earn, label("Population")), ///
			vertical recast(connected) nooffset graphregion(color(white)) ///
			xlabel(1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1") xtitle("Time") ytitle("Employment") ///
			name(suspect_mf, replace) yscale(r(0 1)) ylabel(0(0.2)1)
			gr export "${results}\descrip_suspect_mm_emp.pdf", replace
			gr export "${results}\descrip_suspect_mm_emp.eps", replace

restore 






